<template>
  <vs-modal :show.sync="show" effect="fade" width="700px">
    <div class="modal-header" slot="modal-header">
      <h4 class="modal-title">{{$t('lcd.preview')}}</h4>
    </div>  
    <div class="modal-body" slot="modal-body">
      <div class="preview">
        <canvas v-el:video class="video"></canvas>
      </div>
    </div>
    <div class="modal-footer" slot="modal-footer">
      <button type="button" class="btn btn-default" @click.prevent="onQuit">{{$t('common.quit')}}</button>
    </div>
  </vs-modal>
</template>

<script>
import ebus from 'libs/ebus.js'
import WSAvcPlayer from 'libs/wsAvc.js'

export default {
  props: {
    stream: {
      type: String,
      default: 'preview'
    }
  },
  data () {
    return {
      show: false,
      wsHost: ''
    }
  },
  methods: {
    vidPreviewInit () {
      let wsUrl = `${this.wsHost}/websocket/stream/${this.stream}`
      var _preview = new WSAvcPlayer(this.$els.video, "webgl", 1, 35)
      _preview.connect(wsUrl, function(handler) {
        handler.startPlay()
      })
      this._preview = _preview
    },
    vidPreviewUninit () {
      if (this._preview) {
        this._preview.disconnect()
        this._preview = undefined
      }      
    },
    onShow (val) {
      this.wsHost = val
      this.vidPreviewUninit()
      this.show = true
      this.vidPreviewInit()
    },
    onQuit () {
      this.vidPreviewUninit()
      this.show = false
    }
  },
  ready () {
    ebus.on('show-vid-preview', this.onShow)
  }
}
</script>

<style scoped>
.preview {
  width: 640px;
  height: 360px;
  margin: 0 auto;
}
.video {
  height: 100%; 
  width: 100%;
}
</style>

